﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using lumo.display.object2D;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using lumo;

namespace game.display.effect
{
    public class Smoke : ParticleEmitter
    {
        /// <summary>
        /// Sprite constructor using a texture.
        /// </summary>
        /// <param name="Texture">The texture of the sprite.</param>
        public Smoke(Texture2D Texture = null)
        {
            // Texture for emitter.
            this.Texture = Texture;

            // Update Actions
            AddUpdateAction(UpdateLife);
            AddUpdateAction(UpdateColorFadeInOut, new float[] { 500, 5000, 1.4f, 0.5f });
            AddUpdateAction(UpdateLinearPosition);

            // Emit Particle
            CustomEmitParticleAction(particlelist =>
            {
                int Max = Global.Random.Next(2, 4);
                float Life = Global.Random.Next(5000, 10000);
                for (int i = 0; i < Max; i++)
                {
                    Particle particle = new Particle();

                    particle.Scale = (float)(Global.Random.NextDouble() * 3.0f) + 1.0f;
                    float g = 1.0f - (float)Math.Pow(particle.Scale / 8.0f, 2.0f);
                    particle.Color = new Color(g, g, g);

                    particle.LinearVelocity = Vector2.Multiply(new Vector2(Global.Random.Next(-10, 10), Global.Random.Next(-80, 0)), 0.1f);
                    particle.LinearPosition = this.Position + new Vector2(Global.Random.Next(-4, 4), Global.Random.Next(-4, 4));
                    particle.MaxLife = Life;

                    particlelist.Add(particle);
                }
            }
            );

            // Emit a single particle every 10 milliseconds
            //EmitOnMove();
        }

        /// <summary>
        /// Emit a single smoke particle.
        /// </summary>
        /// <param name="Position">The position of the particle.</param>
        public void Emit(Vector2 Position)
        {
            // Texture for emitter.
            Particle particle = new Particle();

            particle.Scale = (float)(Global.Random.NextDouble() * 3.0f) + 1.0f;
            float g = 1.0f - (float)Math.Pow(particle.Scale / 8.0f, 2.0f);
            particle.Color = new Color(g, g, g);

            particle.LinearVelocity = Vector2.Multiply(new Vector2(Global.Random.Next(-10, 10), Global.Random.Next(-80, 0)), 0.1f);
            particle.LinearPosition = Position + new Vector2(Global.Random.Next(-4, 4), Global.Random.Next(-4, 4));
            particle.MaxLife        = Global.Random.Next(5000, 10000);

            Emit(particle);
        }
    }
}
